﻿using MySql.Data.MySqlClient;
using MySqlX.XDevAPI.Relational;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace homework11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataSet ds = new DataSet();
        int index = 0;
        private int GetNextWordId()
        {
            Random rd = new Random();
            int j = rd.Next() % 4000;
            return j; // 替换为实际的逻辑来获取下一个单词的ID  
        }
        private string getChinese()
        {
            return ds.Tables[0].Rows[index][01].ToString();

        }
        private string getEnglish()
        {
            return ds.Tables[0].Rows[index][0].ToString();
        }
        private void button1_Click(object sender, EventArgs e)//查看答案按钮
        {
            answerLabel.Text = getEnglish();
        }

        private void button2_Click(object sender, EventArgs e)//下一个按钮
        {
            if (textBox1.Text == "")
            {
                /** server = 127.0.0.1或者localhost 代表本机地址; port = 3306 端口号; **/
                /** user 用户名; password 密码; database 数据库名称; **/
                string connstr = "server = localhost; port = 3306; user = root ; password = 123456; database = english_word";
                MySqlConnection conn = new MySqlConnection(connstr);
                try
                {
                    //可能出现异常
                    conn.Open();
                    MySqlDataAdapter adapter = new MySqlDataAdapter("select * from englishword", conn);
                    adapter.Fill(ds);//填充数据到ds中
                    //MessageBox.Show("链接成功!");

                }
                catch (MySqlException ex)
                {
                    //异常则提示异常信息
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    //务必关闭MysqlConnection
                    conn.Close();
                }
                button2.Text = "下一个";
            }
            // 从数据库读取下一个单词的中文词义（这里省略了数据库连接和查询代码）  
            index = GetNextWordId();
            string chineseMeaning = getChinese();

            // 显示中文词义  
            textBox1.Text = chineseMeaning;

            // 清空英文输入框  
            textBox2.Text = "";
            answerLabel.Text = "加油";
            ifCorrectLabel.Text = "回车查看正误";
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)//回车检验答案
        {
            if (e.KeyCode == Keys.Enter)//如果按了回车
            {
                // 获取用户输入的英文单词  
                string userInput = textBox2.Text;
                string answer = getEnglish();
                // 检查用户输入（这里省略了数据库查询和比较代码）  

                // 显示结果  
                if (userInput == answer)
                {
                    ifCorrectLabel.Text = "正确";
                }
                else
                {
                    ifCorrectLabel.Text = "错误";
                }
            }
        }
    }
}
